package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpCpMapper;
import com.ruoyi.system.domain.SpCp;
import com.ruoyi.system.service.ISpCpService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 测评Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-16
 */
@Service
public class SpCpServiceImpl implements ISpCpService 
{
    @Autowired
    private SpCpMapper spCpMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询测评
     * 
     * @param cpId 测评主键
     * @return 测评
     */
    @Override
    public SpCp selectSpCpByCpId(Long cpId)
    {
        return spCpMapper.selectSpCpByCpId(cpId);
    }

    /**
     * 查询测评列表
     * 
     * @param spCp 测评
     * @return 测评
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpCp> selectSpCpList(SpCp spCp)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spCp.setDatascope(datascope);
        if(datascope==4){
            spCp.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spCp.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spCp.setCheckHuiyuanId(0l);
            }

        }
        return spCpMapper.selectSpCpList(spCp);
    }

    @Override
    public List<SpCp> selectSpCpListWeb(SpCp spCp)
    {
        return spCpMapper.selectSpCpList(spCp);
    }

    /**
     * 新增测评
     * 
     * @param spCp 测评
     * @return 结果
     */
    @Override
    public int insertSpCp(SpCp spCp)
    {
        return spCpMapper.insertSpCp(spCp);
    }

    /**
     * 修改测评
     * 
     * @param spCp 测评
     * @return 结果
     */
    @Override
    public int updateSpCp(SpCp spCp)
    {
        return spCpMapper.updateSpCp(spCp);
    }

    /**
     * 批量删除测评
     * 
     * @param cpIds 需要删除的测评主键
     * @return 结果
     */
    @Override
    public int deleteSpCpByCpIds(Long[] cpIds)
    {
        return spCpMapper.deleteSpCpByCpIds(cpIds);
    }

    /**
     * 删除测评信息
     * 
     * @param cpId 测评主键
     * @return 结果
     */
    @Override
    public int deleteSpCpByCpId(Long cpId)
    {
        return spCpMapper.deleteSpCpByCpId(cpId);
    }
}
